
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/08/2013 06:24:52
-- Generated from EDMX file: C:\Users\jshin\Documents\Visual Studio 2012\Projects\Personal\MvcSystemsCommander\MvcSystemsCommander\Models\Entity\MSCEntities.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [EMCommander];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_UserIdentity]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Identities] DROP CONSTRAINT [FK_UserIdentity];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRole_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRole] DROP CONSTRAINT [FK_UserRole_User];
GO
IF OBJECT_ID(N'[dbo].[FK_UserRole_Role]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[UserRole] DROP CONSTRAINT [FK_UserRole_Role];
GO
IF OBJECT_ID(N'[dbo].[FK_AppIdentity_inherits_Identity]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Identities_AppIdentity] DROP CONSTRAINT [FK_AppIdentity_inherits_Identity];
GO
IF OBJECT_ID(N'[dbo].[FK_WindowsIdentity_inherits_Identity]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Identities_WindowsIdentity] DROP CONSTRAINT [FK_WindowsIdentity_inherits_Identity];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Roles]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Roles];
GO
IF OBJECT_ID(N'[dbo].[Tasks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tasks];
GO
IF OBJECT_ID(N'[dbo].[Agents]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Agents];
GO
IF OBJECT_ID(N'[dbo].[Identities]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Identities];
GO
IF OBJECT_ID(N'[dbo].[Identities_AppIdentity]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Identities_AppIdentity];
GO
IF OBJECT_ID(N'[dbo].[Identities_WindowsIdentity]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Identities_WindowsIdentity];
GO
IF OBJECT_ID(N'[dbo].[UserRole]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UserRole];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Username] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Roles'
CREATE TABLE [dbo].[Roles] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Tasks'
CREATE TABLE [dbo].[Tasks] (
    [Id] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'Agents'
CREATE TABLE [dbo].[Agents] (
    [Id] int IDENTITY(1,1) NOT NULL
);
GO

-- Creating table 'Identities'
CREATE TABLE [dbo].[Identities] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [User_Id] int  NOT NULL
);
GO

-- Creating table 'Identities_AppIdentity'
CREATE TABLE [dbo].[Identities_AppIdentity] (
    [Username] nvarchar(max)  NOT NULL,
    [Hashword] nvarchar(max)  NOT NULL,
    [Salt] nvarchar(max)  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Identities_WindowsIdentity'
CREATE TABLE [dbo].[Identities_WindowsIdentity] (
    [Username] nvarchar(max)  NOT NULL,
    [Domain] nvarchar(max)  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'UserRole'
CREATE TABLE [dbo].[UserRole] (
    [Users_Id] int  NOT NULL,
    [Roles_Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Roles'
ALTER TABLE [dbo].[Roles]
ADD CONSTRAINT [PK_Roles]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Tasks'
ALTER TABLE [dbo].[Tasks]
ADD CONSTRAINT [PK_Tasks]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Agents'
ALTER TABLE [dbo].[Agents]
ADD CONSTRAINT [PK_Agents]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Identities'
ALTER TABLE [dbo].[Identities]
ADD CONSTRAINT [PK_Identities]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Identities_AppIdentity'
ALTER TABLE [dbo].[Identities_AppIdentity]
ADD CONSTRAINT [PK_Identities_AppIdentity]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Identities_WindowsIdentity'
ALTER TABLE [dbo].[Identities_WindowsIdentity]
ADD CONSTRAINT [PK_Identities_WindowsIdentity]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Users_Id], [Roles_Id] in table 'UserRole'
ALTER TABLE [dbo].[UserRole]
ADD CONSTRAINT [PK_UserRole]
    PRIMARY KEY NONCLUSTERED ([Users_Id], [Roles_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [User_Id] in table 'Identities'
ALTER TABLE [dbo].[Identities]
ADD CONSTRAINT [FK_UserIdentity]
    FOREIGN KEY ([User_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserIdentity'
CREATE INDEX [IX_FK_UserIdentity]
ON [dbo].[Identities]
    ([User_Id]);
GO

-- Creating foreign key on [Users_Id] in table 'UserRole'
ALTER TABLE [dbo].[UserRole]
ADD CONSTRAINT [FK_UserRole_User]
    FOREIGN KEY ([Users_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Roles_Id] in table 'UserRole'
ALTER TABLE [dbo].[UserRole]
ADD CONSTRAINT [FK_UserRole_Role]
    FOREIGN KEY ([Roles_Id])
    REFERENCES [dbo].[Roles]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserRole_Role'
CREATE INDEX [IX_FK_UserRole_Role]
ON [dbo].[UserRole]
    ([Roles_Id]);
GO

-- Creating foreign key on [Id] in table 'Identities_AppIdentity'
ALTER TABLE [dbo].[Identities_AppIdentity]
ADD CONSTRAINT [FK_AppIdentity_inherits_Identity]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Identities]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'Identities_WindowsIdentity'
ALTER TABLE [dbo].[Identities_WindowsIdentity]
ADD CONSTRAINT [FK_WindowsIdentity_inherits_Identity]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Identities]
        ([Id])
    ON DELETE CASCADE ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------